﻿(function ($) {
    $.login = {
        formMessage: function (msg) {
            $('.login_tips').find('.tips_msg').remove();
            $('.login_tips').append('<div class="tips_msg"><i class="fa fa-question-circle"></i>' + msg + '</div>');
        },
        loginClick: function (e) {
            var $username = $("#username");
            var $password = $("#password");
            if ($username.val() == "") {
                $username.focus();
                $.login.formMessage('请输入用户名。');
                e.preventDefault();
                return false;
            } else if ($password.val() == "") {
                $password.focus();
                $.login.formMessage('请输入登录密码。');
                e.preventDefault();
                return false;
            } else {
                $("#login_button").attr('disabled', 'disabled').find('span').html("loading...");
                $.ajax({
                    url: "/Account/Auth/Login",
                    data: { username: $.trim($username.val()), password: md5($.trim($password.val())) },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.state == "success") {
                            //没有绑定手机号
                            if (data.binding == false) {
                                $("#login_button").attr('disabled', false).find('span').html("登录");
                                
                            } else {
                                $('.login_tips').find('.tips_msg').remove();
                                $("#login_button").find('span').html("登录成功，正在跳转...");
                                window.setTimeout(function () {
                                    window.location.href = "/Account/Auth/Index";
                                }, 500);
                                //登陆成功之后，加载完页面，然后开始记录日志
                                //传递的参数分别为菜单Id,菜单名称，事件类型（1表示登陆）
                                //$.ajax({
                                //    type: 'POST',
                                //    url: '/SysAnalysis/MenuMonitor',
                                //    data: { moduleId: 0, moduleName: "登录", eventType: 1 },
                                //    dataType: "json",
                                //    success: function (data) {
                                //        //不做处理，之后有需求再做处理
                                //    }
                                //});
                            }
                        } else if (data.state == "noExistTrainingInstitution") {
                            $.login.formMessage(data.message);
                            window.location.href = "/Account/Auth/Login";
                        } else {
                            $("#login_button").removeAttr('disabled').find('span').html("登录");
                            $.login.formMessage(data.message);
                        }
                    }
                });
            }
            e.preventDefault();
        },
        init: function () {
            $('.wrapper').height($(window).height());
            $(".container").css("margin-top", ($(window).height() - $(".container").height()) / 2 - 50);
            $(window).resize(function (e) {
                $('.wrapper').height($(window).height());
                $(".container").css("margin-top", ($(window).height() - $(".container").height()) / 2 - 50);
            });
            var login_error = top.$.cookie('huikaoba_login_error');
            if (login_error != null) {
                switch (login_error) {
                    case "overdue":
                        $.login.formMessage("未登录或系统登录已超时,请重新登录");
                        break;
                    case "OnLine":
                        $.login.formMessage("您的帐号已在其它地方登录,请重新登录");
                        break;
                    case "-1":
                        $.login.formMessage("系统未知错误,请重新登录");
                        break;
                }
                top.$.cookie('huikaoba_login_error', '', { path: "/", expires: -1 });
            }
            $("#login_button").click(function (e) {
                $.login.loginClick(e);
            });
            document.onkeydown = function (e) {
                if (!e) e = window.event;
                if ((e.keyCode || e.which) == 13) {
                    document.getElementById("login_button").focus();
                    document.getElementById("login_button").click();
                }
            }
        }
    };
    $(function () {
        if (window != top) {
            top.location.href = location.href;
        }
        $.login.init();
    });
})(jQuery);
